//
//  AppDelegate.swift
//  ElectrickService
//
//  Created by David on 2017/6/15.
//  Copyright © 2017年 David. All rights reserved.
//

import Cocoa

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate, EBServiceDelegate {

    var windowController : NSWindowController?
    var ebService: EBService!
    
    var loginVC: ViewController?
    var mainVC: MainViewController?

    func applicationDidFinishLaunching(_ aNotification: Notification) {
        // Insert code here to initialize your application

        ebService = EBService()
        ebService.delegate = self
        ebService.start()
    }
    
    func showLoginDialog(username: String?, psw: String?, imgData: Data?, codeResult: String?) {
        if windowController == nil{
            windowController = NSStoryboard(name: "Main", bundle: nil).instantiateController(withIdentifier: "LoginWindow") as? NSWindowController
        }
        
        if !(windowController?.window?.isKeyWindow ?? false){
            windowController?.window?.makeKeyAndOrderFront(nil)
        }
        
        if loginVC == nil{
            loginVC = windowController?.contentViewController as? ViewController
        }
        
        
        
        if username != nil{
            loginVC?.unameText.stringValue = username!
            if psw != nil{
                loginVC?.pswText.stringValue = psw!
                
                if codeResult == nil{
                    loginVC?.msgLabel.stringValue = "无法识别验证码，输入验证码来登陆"
                }
            }
        }
        
        if imgData != nil{
            loginVC?.codeImgBtn.image = NSImage(data: imgData!)
        }else{
            loginVC?.msgLabel.stringValue = "无法加载验证码，请点击图片框刷新"
        }
        
        if codeResult != nil{
            loginVC?.verifyText.stringValue = codeResult!
        }
        
    }
    
    func updateCode(imgData: Data?, codeResult: String?) {
        if codeResult != nil{
            loginVC?.verifyText.stringValue = codeResult!
        }else{
            loginVC?.msgLabel.stringValue = "无法识别验证码，输入验证码来登陆"
        }
        if imgData != nil{
            loginVC?.codeImgBtn.image = NSImage(data: imgData!)
        }else{
            loginVC?.msgLabel.stringValue = "无法加载验证码，请点击图片框刷新"
        }
        loginVC?.setCodeLoadingState(false)
    }
    
    func loginSuccess(result: [String]) {
        //loginVC?.setLoadingState(false)
        if windowController == nil{
            windowController = NSStoryboard(name: "Main", bundle: nil).instantiateController(withIdentifier: "LoginWindow") as? NSWindowController
        }
        
        
        
        if mainVC == nil{
            mainVC = NSStoryboard(name: "Main", bundle: nil).instantiateController(withIdentifier: "MainVC") as? MainViewController
        }
        windowController?.contentViewController = mainVC
        
        if !(windowController?.window?.isKeyWindow ?? false){
            windowController?.window?.makeKeyAndOrderFront(nil)
        }
        
    }

    func loginFailes(msg: String?) {
        let showMsg = msg ?? "发生未知错误"
        
        loginVC?.msgLabel.stringValue = showMsg
        loginVC?.setLoadingState(false)
    }
    
    func applicationWillTerminate(_ aNotification: Notification) {
        // Insert code here to tear down your application
    }


}

